package uestc.lj.gulishop.ware.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import uestc.lj.common.exception.ErrorCode;
import uestc.lj.common.exception.NoStockException;
import uestc.lj.common.utils.PageUtils;
import uestc.lj.common.utils.R;
import uestc.lj.gulishop.ware.entity.WareSkuEntity;
import uestc.lj.gulishop.ware.service.WareSkuService;
import uestc.lj.gulishop.ware.vo.SkuHasStockVO;
import uestc.lj.gulishop.ware.vo.WareSkuLockVO;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 商品库存
 *
 * @author crazlee
 * @email peterlee0@163.com
 * @date 2021-05-21 19:33:02
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    @PostMapping("/order/lock")
    public R lockStocks(@RequestBody WareSkuLockVO skuLockVO) {
        try {
            Boolean lockStocked = wareSkuService.lockStocks(skuLockVO);
            return R.ok();
        } catch (NoStockException e) {
            return R.error(ErrorCode.NO_STOCK_EXCEPTION.getCode(), ErrorCode.NO_STOCK_EXCEPTION.getMessage());
        }
    }

    /**
     * 查询sku是否有库存
     *
     * @param skuIds sku商品ID的数组
     * @return
     */
    @PostMapping("/hasStock")
    public R getSkuHasStock(@RequestBody List<Long> skuIds) {
        List<SkuHasStockVO> skuHasStockVOList = wareSkuService.getSkuHasStock(skuIds);
        return R.ok().setData(skuHasStockVOList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id) {
        WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //  @RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //  @RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids) {
        wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
